#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

class UnionFind 
{
public:
    UnionFind(int n) : parent(n + 1) 
    {
        for (int i = 1; i <= n; ++i) 
        {
            parent[i] = i;
        }
    }

    int find(int x) 
    {
        if (x != parent[x]) 
        {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    void unite(int x, int y) 
    {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) 
        {
            parent[rootX] = rootY;
        }
    }

private:
    vector<int> parent;
};

vector<int> findRedundantConnection(vector<vector<int>>& edges)
{
    int n = edges.size();
    UnionFind uf(n);
    vector<int> result;

    for (const auto& edge : edges) 
    {
        int u = edge[0];
        int v = edge[1];

        if (uf.find(u) == uf.find(v)) 
        {
            result = edge;
        }
        else 
        {
            uf.unite(u, v);
        }
    }

    return result;
}

int main() 
{
    int t;
    cin >> t;

    for (int i = 0; i < t; ++i) 
    {
        int n;
        cin >> n;

        vector<vector<int>> edges(n, vector<int>(2));
        for (int j = 0; j < n; ++j) 
        {
            cin >> edges[j][0] >> edges[j][1];
        }

        vector<int> result = findRedundantConnection(edges);
        cout << result[0] << " " << result[1] << endl;
    }

    return 0;
}

